package com.pet.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.entity.Serve;
import com.pet.server.service.IServeService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "ServeServlet", urlPatterns = "/serve")
public class ServeServlet extends HttpServlet {
  @Resource IServeService ss;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String method = request.getParameter("method");

    if (method == null || "".equals(method)) {
      getAllServeListByPage(request, response);
    } else if (method.equalsIgnoreCase("queryByServeType")) {
      findServeByServeType(request, response);
    } else if (method.equalsIgnoreCase("queryByTime")) {
      findServeByTime(request, response);
    } else if (method.equalsIgnoreCase("addNewServe")) {
      addNewServe(request, response);
    } else if (method.equalsIgnoreCase("updateServe")) {
      updateServeSelective(request, response);
    } else if (method.equalsIgnoreCase("delServeSelective")) {
      deleteServeSelective(request, response);
    } else if (method.equalsIgnoreCase("getServeInfos")) {
      getServeInfosById(request, response);
    }
  }

  /** 通过服务id获取服务对象数据 */
  private void getServeInfosById(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取参数 ...
    int serveId = Integer.parseInt(request.getParameter("serveId"));

    // 调用业务接口...
    Serve serve = ss.getServeSelective(serveId);

    //  结果封装 ...
    ResultUtil<Serve> r = null;
    if (serve != null) {
      r = new ResultUtil<>(serve);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 删除选定服务对象数据 */
  private void deleteServeSelective(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取参数
    int serveId = Integer.parseInt(request.getParameter("serveId"));

    // 调用业务接口...
    boolean b = ss.delServeSelectiveById(serveId);

    // 结果封装
    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(b);
    } else {
      r = new ResultUtil<>("删除失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 修改选定服务对象数据 */
  private void updateServeSelective(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取前端json数据,并返回对象
    Serve serve = parseDataFromJson(request, response);

    // 调用业务接口...
    boolean b = ss.updateServeSelective(serve);

    // 结果封装...
    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(b);
    } else {
      r = new ResultUtil<>("修改失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 添加服务数据 */
  private void addNewServe(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取前端json数据,并返回对象
    Serve serve = parseDataFromJson(request, response);

    // 调用业务接口...
    boolean b = ss.addNewServe(serve);

    // 结果封装
    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(b);
    } else {
      r = new ResultUtil<>("添加失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 通过服务排期查询服务 */
  private void findServeByTime(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取查询参数 ...
    String startTime = request.getParameter("startTime");
    String endTime = request.getParameter("endTime");
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));

    // 调用业务接口...
    Page<Serve> list = ss.getServiceListByTime(pageNo, pageSize, shopId, startTime, endTime);

    // 结果封装...
    ResultUtil<Page> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);

    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 通过服务品类查询服务 */
  private void findServeByServeType(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取查询参数...
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    String serveType = request.getParameter("serveType");

    // 调用业务接口...
    Page<Serve> list = ss.getServeListByServeType(pageNo, pageSize, shopId, serveType);

    // 结果封装...
    ResultUtil<Page> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);

    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /** 获取门店所有服务数据 */
  private void getAllServeListByPage(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // 获取查询参数...
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    // 调用业务接口...
    Page<Serve> list = ss.getAllServeList(pageNo, pageSize, shopId);

    // 结果封装...
    ResultUtil<Page> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);

    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /**
   * 通过Jackson的mapper方法 ,生成对象
   *
   * @param request req
   * @param response resp
   * @return 服务对象数据
   */
  private Serve parseDataFromJson(HttpServletRequest request, HttpServletResponse response) {
    // 声明传入数据类型
    response.setContentType("application/json");

    Serve serve = null;

    // 获取reader
    try (BufferedReader reader = request.getReader()) {
      // 读取json数据流
      String json = reader.readLine();

      System.out.println("json = " + json);
      // 通过Jackson的Mapper方法反序列化,生成对象

      serve = new ObjectMapper().readValue(json, Serve.class);

      reader.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
    return serve;
  }
}
